﻿using FCBLL;
using FCModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileConverter.Bas
{
    public partial class BomData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bindGV();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            queryNumByName();
        }

        protected void bindGV() 
        {
            GV_BomData.DataSource = new BaseRepositoryBLL<bas_Bom_Dnum>().LoadAllEntities().ToList<bas_Bom_Dnum>();
            GV_BomData.DataBind();
        }

        protected void queryNumByName()
        {
            if (String.IsNullOrWhiteSpace(tb_OutPrdName.Text.Trim()))
                bindGV();
            else
            {
                GV_BomData.DataSource = new BaseRepositoryBLL<bas_Bom_Dnum>().
                    LoadEntities(p => p.OutPrdName.Equals(tb_OutPrdName.Text.Trim().Trim())).ToList<bas_Bom_Dnum>();
                GV_BomData.DataBind();
            }
        }

        protected void bt_Improt_Click(object sender, EventArgs e)
        {
            string logPath = Server.MapPath("~/Excel/");
            if (FileUpload1.PostedFile.FileName == "" || FileUpload1.PostedFile.InputStream.Length < 1)
            {
                lblInfo.Text = "没有指定文件.";
            }
            else
            {
                try
                {
                    //根据testType获取dataFormatType
                    string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                    string FileName = new FileInfo(FileUpload1.PostedFile.FileName).Name;
                    //  CommonBLL.WriteLog(logPath, "进入程序");
                    if (fileExt.Contains(".xls"))// == ".xlsx" || fileExt == ".xls")
                    {
                        string fileName = Guid.NewGuid().ToString() + fileExt;
                        if (!Directory.Exists(Server.MapPath("~/Excel/")))
                        {
                            Directory.CreateDirectory(Server.MapPath("~/Excel/"));
                        }
                        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Excel/") + fileName);
                        DataSet ds = CommonBLL.ExcelToDS(Server.MapPath("~/Excel/") + fileName);
                        importData(ds);
                        if (System.IO.File.Exists(Server.MapPath("~/Excel/") + fileName))
                        {
                            File.Delete(Server.MapPath("~/Excel/") + fileName);
                        }
                        bindGV();
                    }
                }
                catch (Exception ex) 
                {
                    lblInfo.Text = ex.Message;
                }
            }
        }

        protected void importData(DataSet ds)
        {
            try
            {
                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    string Name = dr[0].ToString().Trim();
                    var Bom = new BaseRepositoryBLL<bas_Bom_Dnum>().LoadEntities(p => p.OutPrdName.Equals(Name)).ToList();
                    if (Bom.Count == 0)
                    {
                        bas_Bom_Dnum bom = new bas_Bom_Dnum();
                        bom.OutPrdName = dr[0].ToString().Trim();
                        bom.PanelCnt = Convert.ToInt32(dr[1].ToString().Trim());
                        new BaseRepositoryBLL<bas_Bom_Dnum>().AddEntities(bom);
                    }
                    else
                    {
                        Bom.FirstOrDefault<bas_Bom_Dnum>().PanelCnt = Convert.ToInt32(dr[1].ToString().Trim());
                        new BaseRepositoryBLL<bas_Bom_Dnum>().UpdateEntities(Bom.FirstOrDefault());
                    }
                }
            }
            catch (Exception ex)
            {
                lblInfo.Text = ex.Message;
            }
        }

        protected void GV_BomData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GV_BomData.PageIndex = e.NewPageIndex;
            GV_BomData.DataBind();
        }

        protected void bt_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrWhiteSpace(tb_OutPrdName.Text))
                {
                    bindGV();
                    lblInfo.Text = ("请输入子板名称后点保存");
                }
                else
                {
                    int num = 0;
                    int.TryParse(tb_PanelCnt.Text.Trim(), out num);
                    var Dnum = new BaseRepositoryBLL<bas_Bom_Dnum>().
                        LoadEntities(p => p.OutPrdName.Equals(tb_OutPrdName.Text.Trim())).ToList<bas_Bom_Dnum>();
                    if (Dnum.Count == 0)
                    {
                        var bom = new bas_Bom_Dnum();
                        bom.OutPrdName = tb_OutPrdName.Text.Trim();
                        bom.PanelCnt = Convert.ToInt32(num);
                        new BaseRepositoryBLL<bas_Bom_Dnum>().AddEntities(bom);
                    }
                    else
                    {
                        Dnum.FirstOrDefault().PanelCnt = Convert.ToInt32(tb_PanelCnt.Text.Trim());
                        new BaseRepositoryBLL<bas_Bom_Dnum>().UpdateEntities(Dnum.FirstOrDefault());
                    }
                }
            }
            catch (Exception ex) 
            {
                lblInfo.Text = ex.Message;
            }
        }
    }
}